package com.zhongkai.controller;

import com.zhongkai.app.service.AppUserService;
import com.zhongkai.model.utils.TTHelp;
import com.zhongkai.model.utils.ZhongkaiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AppUserController {


    @Autowired
    private AppUserService appUserService;

    /**
     * APP用户添加
     *
     * @param name     名字
     * @param phone    手机号
     * @param password 密码
     * @param address  详细地址
     * @param province 省
     * @param city     市
     * @param country  区
     * @param sex      性别
     * @param role     权限id
     */
    @RequestMapping(value = "/app_user/add", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult userAdd(
            String name,
            String phone,
            String password,
            String address,
            String province,
            String city,
            String country,
            String sex,
            @RequestParam(defaultValue = "0") Integer role


    ) {

        String uuid = TTHelp.generate();
        //ZhongkaiResult zhongkaiResult = deviceOperationService.deviceAdd(device_id, device_ip, device_ver, device_code, set_status);
        ZhongkaiResult result = null;
        try {
            result = appUserService.addUser(uuid, name, phone, password, address, province, city, country, sex);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "添加失败");
        }

        return result;
    }


    /**
     * APP用户修改
     *
     * @param id       id
     * @param name     名字
     * @param phone    手机号
     * @param password 密码
     * @param address  详细地址
     * @param province 省
     * @param city     市
     * @param country  区
     * @param sex      性别
     * @param role     权限id
     */

    @RequestMapping(value = "/app_user/update", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult updateUser(
            @RequestParam(defaultValue = "0") Long id,
            String name,
            String phone,
            String password,
            String address,
            String province,
            String city,
            String country,
            String sex,
            @RequestParam(defaultValue = "0") Integer role


    ) {

        //ZhongkaiResult zhongkaiResult = deviceOperationService.deviceAdd(device_id, device_ip, device_ver, device_code, set_status);
        ZhongkaiResult result = appUserService.updateUser(id, name, phone, password, address, province, city, country, sex);

        return result;
    }



    @RequestMapping(value = "/app_user/remove", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult removeUser(
            @RequestParam(defaultValue = "0") Long id



    ) {

        //ZhongkaiResult zhongkaiResult = deviceOperationService.deviceAdd(device_id, device_ip, device_ver, device_code, set_status);
        ZhongkaiResult result = appUserService.removeUser(id);

        return result;
    }




    /**
     * 用户列表
     * @param page 页码
     * @param rows 条数
     */
    @RequestMapping(value = "/app_user/list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult listUser(
            @RequestParam(defaultValue = "0")Integer page, @RequestParam(defaultValue = "0")Integer rows



    ) {

        //ZhongkaiResult zhongkaiResult = deviceOperationService.deviceAdd(device_id, device_ip, device_ver, device_code, set_status);
        ZhongkaiResult result = appUserService.listUser(page,rows);

        return result;
    }
}
